import requests
import json
from itertools import cycle

# 原始数据
data = [
    {"resId": "1jv7bpr40000", "resName": "陈情令"},
    {"resId": "321qg3q80000", "resName": "西虹市首富"},
    {"resId": "3edpdshs0000", "resName": "爱情公寓3"},
    {"resId": "40hmhomg0000", "resName": "西游记"},
    {"resId": "4nnbm68k0000", "resName": "怒海潜沙秦岭神树"},
    {"resId": "4o3qe6sc0000", "resName": "仙剑奇侠传三"},
    {"resId": "4o3rei6c0000", "resName": "甄嬛传"},
    {"resId": "4o3sj5780000", "resName": "长安十二时辰"},
    {"resId": "5hfds5ss0000", "resName": "吞噬星空"},
    {"resId": "601vc0k40400", "resName": "复仇者联盟4终局之战"},
    {"resId": "6vkf1m8g0400", "resName": "庆余年"},
    {"resId": "7b2hstrg0400", "resName": "潜伏"},
    {"resId": "7f9ltq4c0400", "resName": "杨过与小龙女"},
    {"resId": "9g1rjhe00400", "resName": "雪中悍刀行"},
    {"resId": "amvqgc2o0400", "resName": "天下无毒"},
    {"resId": "bb15scmo0400", "resName": "觉醒年代"},
    {"resId": "c78p00ek0400", "resName": "唐人街探案3"},
    {"resId": "d7cqq4mc4400", "resName": "仙逆"},
    {"resId": "d7d329cc8800", "resName": "遮天"},
    {"resId": "dm9it88g8800", "resName": "无间"},
    {"resId": "e1raasro4400", "resName": "峰爆"},
    {"resId": "ea5bnaikcc00", "resName": "侏罗纪世界3"},
    {"resId": "eb8tj0808800", "resName": "完美世界"},
    {"resId": "etpq9kpo4400", "resName": "狂飙"},
    {"resId": "fgrl30b44400", "resName": "昆仑神宫"},
    {"resId": "gv51daa84400", "resName": "罚罪"},
    {"resId": "i82gvu20cc00", "resName": "以爱为营"},
    {"resId": "ik7sj7288800", "resName": "折腰"},
    {"resId": "kbt86eoscc00", "resName": "大奉打更人"},
    {"resId": "lmqdifes4400", "resName": "锦月如歌"},
    {"resId": "lol14luc4400", "resName": "淮水竹亭"},
    {"resId": "me67vn8o8800", "resName": "九重紫"},
    {"resId": "n4dbrebk4400", "resName": "热辣滚烫"},
    {"resId": "na78t2g4cc00", "resName": "有你的时光里"},
    {"resId": "nb5dqmn8cc00", "resName": "绽放的许开心"},
    {"resId": "ndp9hvd04400", "resName": "赴山海"},
    {"resId": "nf3i1k988800", "resName": "凡人修仙传"},
    {"resId": "nf3i1snocc00", "resName": "难哄"},
    {"resId": "nfoap9qscc01", "resName": "黑白诀"},
    {"resId": "njhst388cc00", "resName": "毒液最后一舞"},
    {"resId": "nm4rliag8802", "resName": "陷入我们的热恋"},
    {"resId": "npngmdjccc00", "resName": "无尽的尽头"},
    {"resId": "npnrupbo8800", "resName": "棋士"},
    {"resId": "o7t98or4cc00", "resName": "绝密较量"},
    {"resId": "ockqbhvg4400", "resName": "打黑"},
    {"resId": "ok5jf9k04400", "resName": "熊猫计划"},
    {"resId": "p18bm4d88800", "resName": "焚城"},
    {"resId": "p813d7s8cc02", "resName": "射雕英雄传侠之大者"},
    {"resId": "peoivlpg8800", "resName": "749局"},
    {"resId": "qte1ct1gcc00", "resName": "唐探1900"},
    {"resId": "r7darouc4400", "resName": "制暴无限杀机"},
    {"resId": "r9197lo0000", "resName": "琅琊榜"},
    {"resId": "r99ro2g0000", "resName": "喋血孤岛"},
    {"resId": "r9b0pc80000", "resName": "白鹿原"},
    {"resId": "r9ee9nc0000", "resName": "鬼吹灯之精绝古城"},
    {"resId": "r9kvi4k0000", "resName": "功夫"},
    {"resId": "r9mtl000000", "resName": "斗罗大陆"},
    {"resId": "ret97ie84400", "resName": "扫毒风暴"}
]

tids = [f"tf3dop72v3qi{i}" for i in range(1, 21)]  # 20 个 tid

# 使用 cycle 轮询分配 tid
tid_cycle = cycle(tids)
tid_data_mapping = {}

# 初始化每个 tid 的数据列表
for tid in tids:
    tid_data_mapping[tid] = []

# 轮询分配数据
for item in data:
    current_tid = next(tid_cycle)
    tid_data_mapping[current_tid].append(item)

# 接口调用（同上）
url = "https://icpu.coocaa.com/api/smartTopic/topicItem/save"
headers = {
    'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
    'Content-Type': 'application/json'
}

for tid, batch in tid_data_mapping.items():
    for item in batch:
        payload = json.dumps({
            "tid": tid,
            "resId": item["resId"],
            "resName": item["resName"]
        })

        try:
            response = requests.post(url, headers=headers, data=payload)
            print(f"TID: {tid}, ResID: {item['resId']}, Status: {response.status_code}, Response: {response.text}")
        except Exception as e:
            print(f"Error for TID: {tid}, ResID: {item['resId']}, Error: {str(e)}")